package com.yy.yyui.combobox
{
	import com.maqie.view.UM;
	import com.yy.yyui.list.List;
	import com.yy.yyui.popup.PopUpManager;
	import com.yy.yyui.table.DataGrid;
	import com.yy.yyui.text.TextInput;
	import com.util.tools.GT;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.text.TextFormatAlign;
	
	public class ComboBox extends Sprite
	{
		private var w:Number,h:Number;
		private var _data:Object;
		private var _actions:Object;
		private var _bg:DisplayObject;
		private var _textLabel:TextInput;
		private var _comboBoxButton:SimpleButton;
		private var _popup:DisplayObject;
		private var _defaultList:List;
		private var _popAlign:Object;
		private var _showCount:Number;
		private var _itemHeight:Number;
		
		public static const TOP:Object = "top";
		public static const BOTTOM:Object = "bottom";
		
		
		public function ComboBox(w:Number,h:Number=14,showCount:Number = 5,itemHeight:Number = 20,bg:DisplayObject=null,button:SimpleButton = null,popup:DisplayObject = null,inputEndbel:Boolean = false, scrollBgClass:Class=null)
		{
			//TODO: implement function
			super();
			this.w = w;
			this.h = h;
			this._showCount = showCount;
			this._itemHeight = itemHeight;
			var dobj:DisplayObject;
			
			//background
			if(bg != null){
				bg = UM.scale9(bg);
				bg.width = this.width;
				bg.height = this.height;
				this.addChild(bg);
				this._bg = bg;
			}
			//button;
			var btn:SimpleButton;
			btn = button;
			dobj = this.addChild(btn);
			dobj.x = this.width-dobj.width-1;
			this._comboBoxButton = btn;
			
			//textlabel
			var t:TextInput;
			var rect:Rectangle;
			rect = this.comboBoxButton.getBounds(this);
			t = new TextInput("",w,1,null,TextFormatAlign.LEFT,inputEndbel);
			t.width = this.comboBoxButton.x;
			dobj = this.addChild(t);
			dobj.x = 2;
			dobj.y = -3+(this.height-14)/2;
			this._textLabel = t;
			
			//popup
			if(popup == null){
				var li:List;
				li = new List(this.comboBoxButton.x+this.comboBoxButton.width-this.textLabel.x+10,this.height*showCount-3,_itemHeight,showCount,scrollBgClass);
				this._popup = li;
				li.datagrid.cellPositions[0]=DataGrid.LEFT;
				li.onSelectItem = onListClick;
				this._defaultList = li;
				
			}else{
				this._popup = popup;
			}
			this.popup.visible = false;
			
			
			//set actions		
			this._actions = {};
			this.addEventListener(MouseEvent.CLICK,clickHandler);
			this.addEventListener(Event.REMOVED_FROM_STAGE,destroy);
			this.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
		}
		
		private function onRollOver(e:MouseEvent):void{
			this.removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			this.addEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		private function onRollOut(e:MouseEvent):void{
			this.addEventListener(MouseEvent.ROLL_OVER,onRollOver);
			this.removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
		}
		
		private function destroy(e:Event):void{
			this.removeEventListener(MouseEvent.ROLL_OVER,onRollOver);
			this.removeEventListener(MouseEvent.ROLL_OUT,onRollOut);
			this.removeEventListener(Event.REMOVED_FROM_STAGE,destroy);
			this.removeEventListener(MouseEvent.CLICK,clickHandler);
			if(this.defaultList!=null){
				this.defaultList.destroy();
			}
			if(this.popup.parent!=null){
				this.popup.parent.removeEventListener(MouseEvent.MOUSE_DOWN,hidePopup);				
				this.popup.parent.removeChild(this.popup);
			}
			GT.removeAllChild(this);
			GT.destroyActions(this.actions);
		} 		
		private function onListClick(previousItem:Object,currentItem:Object):void{
			var obj:Object = null;
			if(currentItem!=null){
				obj = this.defaultList.getItem(currentItem as int);
			}
			this.data = obj;
			if(obj==null){
				obj = "";
			}
			this.textLabel.text = obj.toString();
			hidePopup();
		}
		
		
		
		public function clickHandler(e:MouseEvent):void{
			var dobj:DisplayObject;
			if(popup.parent==null){
				var doc:DisplayObjectContainer = PopUpManager.defaultRoot;
				if(doc==null){
					doc = this.stage;
				}
				dobj = doc.addChild(popup);
			}
			popup.visible=!popup.visible;
			if(this.popup==this.defaultList){
				if(this.defaultList.datagrid.rowNum==0){
					popup.visible = false;
				}
			}
			if(this.popup.visible){
				var rect:Rectangle = this.comboBoxButton.getBounds(popup.parent);
				popup.x = rect.right-popup.width;
				rect = this.getBounds(popup.parent);
				if(this.popAlign == TOP)
				{
					if(this.defaultList.datagrid.cellLocations.length < this.showCount)
					{
						popup.y = rect.bottom - this.defaultList.datagrid.cellLocations.length*_itemHeight-13;
					}else
					{
						popup.y = rect.bottom - this.showCount*_itemHeight-13;
					}
				}else
				{
					popup.y = rect.bottom;
				}
				popup.x += 6;
				popup.parent.addEventListener(MouseEvent.MOUSE_DOWN,hidePopup);
			}
		}
		
		public function hidePopup(e:Event=null):void{
			if(e==null){
				this.popup.visible = false;
			}else if(e.type == MouseEvent.MOUSE_DOWN){
				var me:MouseEvent = e as MouseEvent;
				var sx:Number = me.stageX;
				var sy:Number = me.stageY;
				var popupDoc:DisplayObject = e.currentTarget as DisplayObject;
				var rect1:Rectangle =this.popup.getBounds(popupDoc.stage);
				var rect2:Rectangle = this.getBounds(popupDoc.stage);
				if(!(rect1.contains(sx,sy)||rect2.contains(sx,sy))){
					this.popup.visible = false;
				}
			}
			if(!this.popup.visible&&this.popup.parent!=null){
				this.popup.parent.removeEventListener(MouseEvent.CLICK,hidePopup);
			}
			
		}
		
		public function set disableSelect(value:Boolean):void{
			this._comboBoxButton.mouseEnabled = !value;
			this._comboBoxButton.enabled = !value;
			this.mouseEnabled = !value;
			this.mouseChildren = !value;
		}
		
		
		public function set popAlign(param:Object):void
		{
			this._popAlign = param;
		}
		public function get popAlign():Object
		{
			return this._popAlign;
		}
		public function set showCount(param:Number):void
		{
			this._showCount = param;
		}
		public function get showCount():Number
		{
			return this._showCount;
		}
		
		public function get actions():Object{return this._actions;}
		public function get onClick():Function{return this.actions.onClick;}
		public function set onClick(value:Function):void{this.actions.onClick = value;}
		public function get data():Object{return _data;}
		public function set data(value:Object):void
		{
			this._data = value;
		}		
		
		public function get comboBoxButton():SimpleButton{return this._comboBoxButton;}
		public function get bg():DisplayObject{return this._bg;}
		public function get textLabel():TextInput{return this._textLabel;}
		public function get popup():DisplayObject{return this._popup;}
		public function get defaultList():List{return this._defaultList;}
		
		
		override public function get width():Number{return w;}
		override public function get height():Number{return h;}		
		
		override public function set width(newWidth : Number) : void {
			var diff:Number = newWidth - w;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			this.comboBoxButton.x += diff;
			w = newWidth;
		}
		
		override public function set height(newHeight : Number) : void {
			var diff:Number = newHeight - h;
			var oldW:Number,oldH:Number;
			var i:uint,l:uint;
			this.bg.height+=diff;
			this.comboBoxButton.y += diff/2;
			this.textLabel.height += diff/2;
			this.textLabel.y += diff/2;
			h = newHeight;			
		}		
	}
}